package com.vonchange.utils.validate;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

/**
 * @ClassName: ValidateUtils
 * @Description: TODO 校验工具类
 * @author lindapeng
 * @date 2014-7-29 下午2:52:28
 */
public class ValidateUtils {
	/**
	 * 匹配效验
	 * @param value
	 * @param el
	 * @return
	 * @since hipai　Ver 1.1
	 */
	public static boolean validate(String value, String el) {
		Pattern p = Pattern.compile(el);
		Matcher m = p.matcher(value);
		return m.matches();
	}

	/**
	 * @Title: dataISO
	 * @Description: TODO 校验时间格式是否正确
	 * @param checkValue
	 * @return
	 */
	public static boolean dataISO(String checkValue) {
		String eL = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]" + "?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]" + "?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))" + "[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|" + "(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1][0-9])|([2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";
		Pattern p = Pattern.compile(eL);
		Matcher m = p.matcher(checkValue);
		return m.matches();
	}

	/**
	 * @Title: dataYear_MM
	 * @Description: TODO 校验年月格式是否正确
	 * @param checkValue
	 * @return
	 */
	public static boolean dataYear_MM(String checkValue) {
		String eL = "[0-9]{4}-[0-9]{2}";
		Pattern p = Pattern.compile(eL);
		Matcher m = p.matcher(checkValue);
		return m.matches();
	}

	/**
	 * @Title: nubmer
	 * @Description: TODO 是否合法数字
	 * @param checkValue
	 * @return
	 */
	public static boolean nubmer(String checkValue) {
		String el = "^-?(?:\\d+|\\d{1,3}(?:,\\d{3})+)?(?:\\.\\d+)?$";
		Pattern p = Pattern.compile(el);
		Matcher m = p.matcher(checkValue);
		return m.matches();
	}

	/**
	 * @Title: decimal_2p
	 * @Description: TODO 输入非负整数或小数（小数最多精确到小数点后2位）
	 * @param checkValue
	 * @return
	 */
	public static boolean decimal_2p(String checkValue) {
		if (nubmer(checkValue)) {
			String el = "^[0-9]+([.]{1}[0-9]{1,2})?$";
			Pattern p = Pattern.compile(el);
			Matcher m = p.matcher(checkValue);
			return m.matches();
		} else {
			return false;
		}
	}

	/**
	 * @Title: digits
	 * @Description: TODO 是否零或正整数
	 * @param checkValue
	 * @return
	 */
	public static boolean digits(String checkValue) {
		String el = "^[0-9]+$";
		Pattern p = Pattern.compile(el);
		Matcher m = p.matcher(checkValue);
		return m.matches();
	}

	/**
	 * @Title: maxLength
	 * @Description: TODO 检查输入是否 超出规定长度
	 * @param value
	 * @param length
	 * @return
	 */
	public static boolean maxLength(String value, int length) {
		return ((value == null || "".equals(value.trim())) ? 0 : value.length()) < length;
	}

	/**
	 * @Title: en_Num
	 * @Description: TODO 输入英文和数字
	 * @param checkValue
	 * @return
	 */
	public static boolean en_Num(String checkValue) {
		String el = "^[A-Za-z0-9]+$";
		Pattern p = Pattern.compile(el);
		Matcher m = p.matcher(checkValue);
		return m.matches();
	}

	/**
	 * @Title: en_Zh
	 * @Description: TODO 输入英文和中文
	 * @param checkValue
	 * @return
	 */
	public static boolean en_Zh(String checkValue) {
		String el = "^[a-zA-Z\u4e00-\u9fa5]+$";
		Pattern p = Pattern.compile(el);
		Matcher m = p.matcher(checkValue);
		return m.matches();
	}

	/**
	 * @Title: en_Zh_Num
	 * @Description: TODO 输入英文、数字和数字
	 * @param checkValue
	 * @return
	 */
	public static boolean en_Zh_Num(String checkValue) {
		if (StringUtils.isEmpty(checkValue))
			return true;
		String el = "^[a-zA-Z0-9\u4e00-\u9fa5]+$";
		Pattern p = Pattern.compile(el);
		Matcher m = p.matcher(checkValue);
		return m.matches();
	}

	/**
	 * @Title: mobilePhone
	 * @Description: TODO 输入一个移动电话
	 * @param checkValue
	 * @return
	 */
	public static boolean phone(String checkValue) {
		String el = "^1[3|4|5|7|8][0-9]\\d{4,8}$";
		Pattern p = Pattern.compile(el);
		Matcher m = p.matcher(checkValue);
		return m.matches();
	}

	/**
	 * @Title: phone
	 * @Description: TODO 输入一个固定电话或者移动电话
	 * @param checkValue
	 * @return
	 */
	public static boolean tel(String checkValue) {
		String el = "^((((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\\d{8})|((([0\\+]\\d{2,3}-)?(0\\d{2,3})-)(\\d{7,8})(-(\\d{3,}))?))?$";
		Pattern p = Pattern.compile(el);
		Matcher m = p.matcher(checkValue);
		return m.matches();
	}

	/**
	 * @Title: card
	 * @Description: TODO 身份证号格式是否正确
	 * @param checkValue
	 * @return
	 */
	public static boolean card(String checkValue) {
		String el = "(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])";
		// if (checkValue.length() == 15) {
		// el = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
		// } else if (checkValue.length() == 18) {
		// el =
		// "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$";
		// }
		Pattern p = Pattern.compile(el);
		Matcher m = p.matcher(checkValue);
		return m.matches();
	}

	/**
	 * @Title: range
	 * @Description: TODO 输入的整数只允许在{minValue}-{maxValue}之间
	 * @param checkValue
	 * @param minValue
	 * @param maxValue
	 * @return
	 */
	public static boolean range(String checkValue, int minValue, int maxValue) {
		if (digits(checkValue)) {
			Double value = Double.parseDouble(checkValue);
			return value >= minValue && value <= maxValue;
		}
		return false;
	}

	/**
	 * @Title: range
	 * @Description: TODO 输入的小数只允许在{minValue}-{maxValue}之间
	 * @param checkValue
	 * @param minValue
	 * @param maxValue
	 * @return
	 */
	public static boolean range(String checkValue, BigDecimal minValue, BigDecimal maxValue) {
		BigDecimal value = new BigDecimal(checkValue);
		return (value.compareTo(minValue.setScale(2, BigDecimal.ROUND_HALF_UP)) == 1 || value.compareTo(minValue.setScale(2, BigDecimal.ROUND_HALF_UP)) == 0) && (value.compareTo(maxValue.setScale(2, BigDecimal.ROUND_HALF_UP)) == -1 || value.compareTo(maxValue.setScale(2, BigDecimal.ROUND_HALF_UP)) == 0);
	}

	/**
	 * @Title: required
	 * @Description: TODO 必填项
	 * @param object
	 * @return
	 */
	public static boolean required(Object object) {
		return (object != null && !object.equals("") && !object.equals("null"));
	}

	/**
	 * @Title: compareSumNum
	 * @Description: TODO 比较两个数
	 * @param obj
	 *            可以传三个参数，参数[1] 表示第一个数字，参数[2] 表示第二个数字，参数[3]表示符号< 、<= 、 > 、 >= 、
	 *            ==
	 * @return
	 */
	public static boolean compareSumNum(Object... obj) {
		if (required(obj)) {
			if (required(obj[0]) && required(obj[1]) && required(obj[2])) {
				BigDecimal bd1 = new BigDecimal(obj[0].toString());
				BigDecimal bd2 = new BigDecimal(obj[1].toString());
				if (obj[2].toString().equals("<")) {
					return (bd1.compareTo(bd2) == -1) ? true : false;
				} else if (obj[2].toString().equals("<=")) {
					return (bd1.compareTo(bd2) == -1 || bd1.compareTo(bd2) == 0) ? true : false;
				} else if (obj[2].toString().equals(">")) {
					return (bd1.compareTo(bd2) == 1) ? true : false;
				} else if (obj[2].toString().equals(">=")) {
					return (bd1.compareTo(bd2) == 1 || bd1.compareTo(bd2) == 0) ? true : false;
				} else if (obj[2].toString().equals("==")) {
					return (bd1.compareTo(bd2) == 0) ? true : false;
				}
			}
		}
		return false;
	}

	/**
	 * @Title: email
	 * @Description: 电子邮箱格式验证
	 * @author wuxiaolong
	 * @return boolean
	 */
	public static boolean email(String eamil) {
		final String pattern1 = "^(\\w-*\\.*)+@(\\w-?)+(\\.\\w{2,})+$";
		final Pattern pattern = Pattern.compile(pattern1);
		final Matcher mat = pattern.matcher(eamil);
		return mat.matches();
	}

	public static void main(String[] args) {
		System.out.println(ValidateUtils.card("37060219920205161"));
	}
}
